@extends('admin.layout')

@section('title', '编辑分类')
@section('page-title', '编辑分类')

@section('content')
@php
    $errors = $errors ?? new \Illuminate\Support\MessageBag();
@endphp
<div class="max-w-2xl mx-auto">
    <div class="bg-white shadow-lg rounded-xl border border-gray-100">
        <div class="px-6 py-6 border-b border-gray-200">
            <div class="flex items-center">
                <svg class="w-6 h-6 text-indigo-600 mr-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"></path>
                </svg>
                <h1 class="text-xl font-semibold text-gray-900">编辑分类</h1>
            </div>
            <p class="mt-2 text-sm text-gray-600">修改分类信息</p>
        </div>

        <form method="POST" action="{{ route('admin.categories.update', $category->id) }}" class="px-6 py-6">
            @csrf
            @method('PUT')
            
            <div class="space-y-6">
                <!-- 分类名称 -->
                <div>
                    <label for="name" class="block text-sm font-medium text-gray-700 mb-2">
                        分类名称 <span class="text-red-500">*</span>
                    </label>
                    <input type="text" 
                           name="name" 
                           id="name" 
                           value="{{ old('name', $category->name) }}"
                           required
                           placeholder="例如：热菜、凉菜、汤品等"
                           class="block w-full px-4 py-3 border border-gray-300 rounded-lg shadow-sm placeholder-gray-400 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 transition-colors duration-200 {{ $errors->has('name') ? 'border-red-300' : '' }}">
                    @if($errors->has('name'))
                        <p class="mt-2 text-sm text-red-600">{{ $errors->first('name') }}</p>
                    @endif
                </div>

                <!-- 分类标识 -->
                <div>
                    <label for="slug" class="block text-sm font-medium text-gray-700 mb-2">
                        分类标识 <span class="text-red-500">*</span>
                    </label>
                    <input type="text" 
                           name="slug" 
                           id="slug" 
                           value="{{ old('slug', $category->slug) }}"
                           required
                           placeholder="例如：hot-dishes、cold-dishes等"
                           class="block w-full px-4 py-3 border border-gray-300 rounded-lg shadow-sm placeholder-gray-400 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 transition-colors duration-200 {{ $errors->has('slug') ? 'border-red-300' : '' }}">
                    <p class="mt-2 text-sm text-gray-500">用于URL和API的唯一标识，建议使用英文和连字符</p>
                    @if($errors->has('slug'))
                        <p class="mt-2 text-sm text-red-600">{{ $errors->first('slug') }}</p>
                    @endif
                </div>

                <!-- 分类描述 -->
                <div>
                    <label for="description" class="block text-sm font-medium text-gray-700 mb-2">
                        分类描述
                    </label>
                    <textarea name="description" 
                              id="description" 
                              rows="4"
                              placeholder="描述这个分类的特点和用途..."
                              class="block w-full px-4 py-3 border border-gray-300 rounded-lg shadow-sm placeholder-gray-400 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 transition-colors duration-200 {{ $errors->has('description') ? 'border-red-300' : '' }}">{{ old('description', $category->description) }}</textarea>
                    @if($errors->has('description'))
                        <p class="mt-2 text-sm text-red-600">{{ $errors->first('description') }}</p>
                    @endif
                </div>

                <!-- 排序权重 -->
                <div>
                    <label for="sort_order" class="block text-sm font-medium text-gray-700 mb-2">
                        排序权重
                    </label>
                    <input type="number" 
                           name="sort_order" 
                           id="sort_order" 
                           value="{{ old('sort_order', $category->sort_order) }}"
                           min="0"
                           placeholder="数字越小排序越靠前"
                           class="block w-full px-4 py-3 border border-gray-300 rounded-lg shadow-sm placeholder-gray-400 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 transition-colors duration-200 {{ $errors->has('sort_order') ? 'border-red-300' : '' }}">
                    <p class="mt-2 text-sm text-gray-500">用于控制分类在列表中的显示顺序</p>
                    @if($errors->has('sort_order'))
                        <p class="mt-2 text-sm text-red-600">{{ $errors->first('sort_order') }}</p>
                    @endif
                </div>

                <!-- 启用状态 -->
                <div>
                    <div class="flex items-center">
                        <input type="checkbox" 
                               name="is_active" 
                               id="is_active" 
                               value="1"
                               {{ old('is_active', $category->is_active) ? 'checked' : '' }}
                               class="h-4 w-4 text-indigo-600 focus:ring-indigo-500 border-gray-300 rounded">
                        <label for="is_active" class="ml-3 block text-sm font-medium text-gray-700">
                            启用此分类
                        </label>
                    </div>
                    <p class="mt-2 text-sm text-gray-500">禁用的分类不会在前端显示</p>
                </div>

                <!-- 分类信息 -->
                <div class="bg-gray-50 rounded-lg p-4">
                    <h3 class="text-sm font-medium text-gray-900 mb-3">分类信息</h3>
                    <div class="grid grid-cols-2 gap-4 text-sm">
                        <div>
                            <span class="text-gray-500">菜品数量：</span>
                            <span class="font-medium text-gray-900">{{ $category->dish_count }}</span>
                        </div>
                        <div>
                            <span class="text-gray-500">创建时间：</span>
                            <span class="font-medium text-gray-900">{{ $category->created_at->format('Y-m-d H:i') }}</span>
                        </div>
                        <div>
                            <span class="text-gray-500">更新时间：</span>
                            <span class="font-medium text-gray-900">{{ $category->updated_at->format('Y-m-d H:i') }}</span>
                        </div>
                        <div>
                            <span class="text-gray-500">当前状态：</span>
                            <span class="font-medium {{ $category->is_active ? 'text-green-600' : 'text-red-600' }}">
                                {{ $category->is_active ? '启用' : '禁用' }}
                            </span>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 操作按钮 -->
            <div class="flex items-center justify-end space-x-4 pt-6 border-t border-gray-200 mt-8">
                <a href="{{ route('admin.categories.index') }}" 
                   class="inline-flex items-center px-6 py-3 border border-gray-300 rounded-lg shadow-sm text-sm font-medium text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 transition-colors duration-200">
                    <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path>
                    </svg>
                    取消
                </a>
                <button type="submit" 
                        class="inline-flex items-center px-6 py-3 border border-transparent rounded-lg shadow-sm text-sm font-medium text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 transition-colors duration-200">
                    <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"></path>
                    </svg>
                    更新分类
                </button>
            </div>
        </form>
    </div>

    <!-- 该分类下的菜品列表 -->
    @if($dishes->count() > 0)
    <div class="mt-8 bg-white shadow-lg rounded-xl border border-gray-100">
        <div class="px-6 py-6 border-b border-gray-200">
            <div class="flex items-center justify-between">
                <div class="flex items-center">
                    <svg class="w-6 h-6 text-indigo-600 mr-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 6.253v13m0-13C10.832 5.477 9.246 5 7.5 5S4.168 5.477 3 6.253v13C4.168 18.477 5.754 18 7.5 18s3.332.477 4.5 1.253m0-13C13.168 5.477 14.754 5 16.5 5c1.746 0 3.332.477 4.5 1.253v13C19.832 18.477 18.246 18 16.5 18c-1.746 0-3.332.477-4.5 1.253"></path>
                    </svg>
                    <h2 class="text-xl font-semibold text-gray-900">该分类下的菜品</h2>
                </div>
                <div class="flex items-center space-x-3">
                    <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-indigo-100 text-indigo-800">
                        共 {{ $dishes->count() }} 个菜品
                    </span>
                    <a href="{{ route('admin.dishes.index') }}?category_id={{ $category->id }}" 
                       class="inline-flex items-center px-4 py-2 border border-transparent rounded-lg shadow-sm text-sm font-medium text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 transition-colors duration-200">
                        <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 6H6a2 2 0 00-2 2v10a2 2 0 002 2h10a2 2 0 002-2v-4M14 4h6m0 0v6m0-6L10 14"></path>
                        </svg>
                        查看全部
                    </a>
                </div>
            </div>
            <p class="mt-2 text-sm text-gray-600">管理该分类下的所有菜品</p>
        </div>

        <div class="overflow-x-auto">
            <table class="min-w-full divide-y divide-gray-200">
                <thead class="bg-gray-50">
                    <tr>
                        <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                            菜品信息
                        </th>
                        <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                            状态
                        </th>
                        <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                            操作
                        </th>
                    </tr>
                </thead>
                <tbody class="bg-white divide-y divide-gray-200">
                    @foreach($dishes as $dish)
                    <tr class="hover:bg-gray-50">
                        <td class="px-6 py-4 whitespace-nowrap">
                            <div class="text-sm font-medium text-gray-900">{{ $dish->name }}</div>
                            <div class="text-sm text-gray-500">{{ $dish->description }}</div>
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap">
                            @if($dish->is_featured)
                                <span class="inline-flex items-center px-2 py-1 rounded-full text-xs font-medium bg-yellow-100 text-yellow-800">
                                    推荐
                                </span>
                            @else
                                <span class="text-sm text-gray-500">普通</span>
                            @endif
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
                            <div class="flex items-center space-x-2">
                                <a href="{{ route('admin.dishes.edit', $dish->id) }}" 
                                   class="text-indigo-600 hover:text-indigo-900">
                                    编辑
                                </a>
                                <form method="POST" action="{{ route('admin.dishes.toggle-featured', $dish->id) }}" class="inline">
                                    @csrf
                                    <button type="submit" 
                                            class="{{ $dish->is_featured ? 'text-yellow-600 hover:text-yellow-900' : 'text-gray-600 hover:text-gray-900' }}">
                                        {{ $dish->is_featured ? '取消推荐' : '设为推荐' }}
                                    </button>
                                </form>
                            </div>
                        </td>
                    </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
    </div>
    @else
    <div class="mt-8 bg-white shadow-lg rounded-xl border border-gray-100">
        <div class="px-6 py-8 text-center">
            <svg class="mx-auto h-16 w-16 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 6.253v13m0-13C10.832 5.477 9.246 5 7.5 5S4.168 5.477 3 6.253v13C4.168 18.477 5.754 18 7.5 18s3.332.477 4.5 1.253m0-13C13.168 5.477 14.754 5 16.5 5c1.746 0 3.332.477 4.5 1.253v13C19.832 18.477 18.246 18 16.5 18c-1.746 0-3.332.477-4.5 1.253"></path>
            </svg>
            <h3 class="mt-4 text-lg font-medium text-gray-900">该分类下暂无菜品</h3>
            <p class="mt-2 text-sm text-gray-500">开始为这个分类添加菜品吧。</p>
            <div class="mt-6">
                <a href="{{ route('admin.dishes.create') }}?category_id={{ $category->id }}" 
                   class="inline-flex items-center px-4 py-2 border border-transparent shadow-sm text-sm font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500">
                    <svg class="-ml-1 mr-2 h-5 w-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 6v6m0 0v6m0-6h6m-6 0H6"></path>
                    </svg>
                    添加菜品
                </a>
            </div>
        </div>
    </div>
    @endif
</div>
@endsection
